package com.example.a2032.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.a2032.R;
import com.example.a2032.Utils.Utils;
import com.example.a2032.entity.ImResultData;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ChatItemAdapter extends RecyclerView.Adapter {

    List<ImResultData.BindersBean> contactList;
    String[] knshipArray;
    int[] iconArrays;

    public ChatItemAdapter(List<ImResultData.BindersBean> contactList, String[] knshipArray, int[] iconArrays) {
        this.contactList = contactList;
        this.knshipArray = knshipArray;
        this.iconArrays = iconArrays;
    }

    public void clear() {
        this.contactList.clear();
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ContactViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contact, parent, false));
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ContactViewHolder settingViewHolder = (ContactViewHolder) holder;
        ImResultData.BindersBean contact = contactList.get(position);
        int contactType = contact.getKinship();
        String name = getShipName(contactType);
        settingViewHolder.tvSettingItem.setText(name);
        settingViewHolder.ivSettingItem.setImageResource(iconArrays[contactType]);
        settingViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemClickListener != null) itemClickListener.onItemClick(contact);
            }
        });
    }

    private String getShipName(int shipId) {

        return knshipArray[shipId];
    }

    @Override
    public int getItemCount() {
        return contactList.size();
    }

    static class ContactViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_setting_item)
        ImageView ivSettingItem;
        @BindView(R.id.tv_setting_item)
        TextView tvSettingItem;

        public ContactViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    onItemClickListener itemClickListener;

    public void setOnItemClickListener(onItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public interface onItemClickListener {
        public void onItemClick(ImResultData.BindersBean contact);
    }

    private static final String TAG = "ChatItemAdapter";

    public void updateData(List<ImResultData.BindersBean> contactList) {
        if (contactList == null) {
            this.contactList.clear();
        } else {
            this.contactList = contactList;
        }
        notifyDataSetChanged();
    }
}
